--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--[[

    生命量产

]]--
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

return function(inst)
    if not TheWorld.ismastersim then
        return
    end
    ------------------------------------------------------------------------------------
    --- 初始化类型
        local save_index = "loramia_spell_mass_production_for_life_worker_records"
        local worker_prefabs = {"shadowlumber","shadowminer","shadowdigger"}
        local worker_prefabs_idx = {}
        for k, v in pairs(worker_prefabs) do
            worker_prefabs_idx[v] = k
        end
    ------------------------------------------------------------------------------------
    --- 初始化 最大可跟随数量
        local need_to_remove_for_init = true
        inst:ListenForEvent("loramia_master_postinit",function()
            inst.components.petleash.maxpets = 3
            ---- 处理初始化问题。带着宠物的存档会 在这里各种崩溃。必须移除。
            local old_ondespawnfn = inst.components.petleash.ondespawnfn
            local new_ondespawnfn = function(inst,pet)
                if pet and worker_prefabs_idx[pet.prefab] and need_to_remove_for_init then                
                    pet:Remove()
                else
                    old_ondespawnfn(inst,pet)
                end
                -- pcall(old_ondespawnfn,inst,pet)
            end
            inst.components.petleash.ondespawnfn = new_ondespawnfn

            local old_onspawnfn = inst.components.petleash.onspawnfn
            local new_onspawnfn = function(inst,pet)
                if pet and worker_prefabs_idx[pet.prefab] and need_to_remove_for_init then
                    pet:Remove()
                else
                    old_onspawnfn(inst,pet)
                end
            end
            inst.components.petleash.onspawnfn = new_onspawnfn
        end)
        inst:DoTaskInTime(0,function()
            need_to_remove_for_init = false
        end)
    ------------------------------------------------------------------------------------
    --- 储存表
        local workers = {}
    ------------------------------------------------------------------------------------
    --- 创建函数
        local function SpawnSingleWorker(prefab,index,record,pt,type_change)
            if workers[index] and workers[index]:IsValid() then
                workers[index].Transform:SetPosition(pt.x,0,pt.z)
                return workers[index]
            end
            -- if not type_change then
            --     if inst.components.sanity.current > 20
            --         and inst.components.health > 20
            -- end
            local pet = inst.components.petleash:SpawnPetAt(pt.x,0,pt.z,prefab)
            workers[index] = pet
            pet.worker_index = index
            pet:AddDebuff("loramia_spell_mass_production_for_life_debuff","loramia_spell_mass_production_for_life_debuff")
            pet.sg:GoToState("idle")
            pet:ListenForEvent("player_despawn",function()
                -- local save_for_join = inst.components.loramia_data:Get(save_index) or {}
                -- table.insert(save_for_join,pet.prefab)
                pet:Remove()
            end,inst)            
            return pet
        end
    ------------------------------------------------------------------------------------
    ---- 新进存档
        -- inst:DoTaskInTime(1,function()
        --     local save_for_join = inst.components.loramia_data:Get(save_index)
        --     inst.components.loramia_data:Set(save_index,nil)
        --     local pt = Vector3(inst.Transform:GetWorldPosition())
        --     if type(save_for_join) == "table" then
        --         for index,prefab in pairs(save_for_join) do
        --             SpawnSingleWorker(prefab,index,nil,pt)
        --         end
        --     end
        -- end)
    ------------------------------------------------------------------------------------
    ---- 用来切换单个类别的。
        inst:ListenForEvent("loramia_spell_mass_production_for_life_type_change",function(inst,_table)
            -- _table = _table or {
            --     target = inst,
            --     target_prefab = nil
            -- }
            local target = _table and _table.target
            local target_prefab = _table and _table.target_prefab
            if not (target and target:IsValid() and target_prefab and worker_prefabs_idx[target_prefab] )then
                return
            end
            ---------------------------------------------------------------
            local pt = Vector3(target.Transform:GetWorldPosition())
            local current_health = target.components.health.currenthealth
            ---------------------------------------------------------------
            target:Remove()
            local index = target.worker_index            
            if index then
                workers[index] = nil
                local temp_worker = SpawnSingleWorker(target_prefab,index,nil,pt,true)
                temp_worker.components.health.currenthealth = current_health
            end
        end)
    ------------------------------------------------------------------------------------
    --- 技能激活，刷3个
        inst:ListenForEvent("loramia_spell_mass_production_for_life_acitved",function(_,pt)
                local points = TUNING.LORAMIA_FN:GetSurroundPoints({
                    target = pt,
                    range = 6,
                    num = 3
                })
                for index = 1,#points do
                    local pt = points[index]
                    local prefab = worker_prefabs[index]
                    local pet = SpawnSingleWorker(prefab,index,nil,pt)
                    if pet then
                        SpawnPrefab("spawn_fx_small").Transform:SetPosition(pt.x,0,pt.z)
                    end                
                end
        end)
    ------------------------------------------------------------------------------------
    -- 链接玩家事件。
        inst:ListenForEvent("loramia_spell_mass_production_for_life_link",function(_,target)        
            local index = worker_prefabs_idx[target.prefab]
            workers[index] = target
        end)
    ------------------------------------------------------------------------------------



end